/*
 * token.c
 *
 *  Created on: 2013-5-4
 *      Author: xkaying
 */

#include "token.h"

#include <assert.h>
#include <stdlib.h>

Token *TokenInit(int type)
{
	Token *ptoken;
	ptoken = (Token*) malloc(sizeof(Token));
	ptoken->len = 0;
	ptoken->line = 0;
	ptoken->name = 0;
	ptoken->type = type;
	return ptoken;
}

int TokenDeclareConvert(Token *ptoken)
{
	assert(ptoken);
	int x;
	x=0;
	if (ptoken->type == TokenType_String) {
		x = ptoken->name[0];
	}
	else if (ptoken->type == TokenType_Value) {
		if ((ptoken->name[0] >= '0') && (ptoken->name[0] <= '9')) {
			x = ptoken->name[0] - '0';
		}
		else if (ptoken->name[0] >= 'a' && ptoken->name[0] <= 'f') {
			x = ptoken->name[0] - 'a';
		}
		else {
			x = ptoken->name[0] - 'A';
		}
		if (ptoken->len > 1) {
			x = x * 16;
			if ((ptoken->name[1] >= '0') && (ptoken->name[1] <= '9')) {
				x += (ptoken->name[1] - '0');
			}
			else if (ptoken->name[1] >= 'a' && ptoken->name[1] <= 'f') {
				x += (ptoken->name[1] - 'a');
			}
			else {
				x += (ptoken->name[0] - 'A');
			}
		}

	}

	if(x>=256){
		x=255;
	}
	if(x<0){
		x=0;
	}
	return x;
}
